package com.iraeta.controller.kcpd;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.Plan;
import com.iraeta.entity.main.type.PlanPdlx;
import com.iraeta.entity.main.type.PlanStatus;
import com.iraeta.entity.main.type.PlanYwlx;
import com.iraeta.entity.main.type.PlanZq;
import com.iraeta.entity.main.vo.PlanVo;
import com.iraeta.service.basic.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController(value = "PlanController")
@RequestMapping("plan")
public class PlanController extends BaseController {
    @Autowired
    PlanService planService;

    @RequestMapping("pageplan")
    public String pagePlan(@RequestBody JSONObject jo){
        String title = jo.getString("title");
        this.pageSet(jo);
        List<PlanVo> planVos = planService.pagePlan(title);
        PageInfo<PlanVo> pageInfo = this.getPageInfo(planVos);
        return this.success(pageInfo);
    }

    @RequestMapping("pageuserplan")
    public String pageUserPlan(@RequestBody JSONObject jo){
        String title = jo.getString("title");
        this.pageSet(jo);
        List<PlanVo> planVos = planService.PageSelfPlan(title, this.getUserId());
        PageInfo<PlanVo> pageInfo = this.getPageInfo(planVos);
        return this.success(pageInfo);
    }

    @RequestMapping("getplanonly")
    public String getPlanOnly(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        Plan plan = planService.getPlan(planId);
        return this.success(plan);
    }

    @RequestMapping("getplan")
    public String getPlan(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        Plan plan = planService.getPlan(planId);
        PlanVo planVo = new PlanVo();
        BeanUtil.copyProperties(plan, planVo, true);
        planVo.setStatusName(PlanStatus.getStatusName(plan.getStatusid()));
        planVo.setPdlxName(PlanPdlx.getPlanPdlxName(plan.getPdlx()));
        planVo.setYwlxName(PlanYwlx.getPlanYwlxName(plan.getYwlx()));
        planVo.setZqName(PlanZq.getPlanZqName(plan.getZq()));
        List<HashMap<String, Object>> plancangkus = planService.getPlanCangKu(planId);
        JSONObject jb = new JSONObject();
        jb.put("plan", planVo);
        jb.put("plancangkus", plancangkus);
        return this.success(jb);
    }

    @RequestMapping("addplan")
    public String addPlan(@RequestBody JSONObject paramJb){
        Plan plan = new Plan();
        this.mapJsonToObj(paramJb, plan);
        if(plan.getCdefine1().compareTo(plan.getCdefine2()) > 0){
            throw new ServiceException("盘点开始日期应该小于盘点截止日期");
        }
        if(plan.getPdlx() == PlanPdlx.choujian){
            if(StrUtil.isEmpty(plan.getCdefine4())){
                throw new ServiceException("请填写最小盘点比率");
            }
            if(StrUtil.isEmpty(plan.getCdefine5())){
                throw new ServiceException("请填写最大重复比率");
            }
        }
        plan.setCreatetime(new Date());
        plan.setCreateuserid(this.getUserId());
        plan.setCreateuser(this.getUserName());
        plan.setStatusid(PlanStatus.add);
        JSONArray selectedCkList = paramJb.getJSONArray("selectedCkIds");
        planService.addPlan(plan, selectedCkList);
        return this.success();
    }

    @RequestMapping("updateplan")
    public String updatePlan(@RequestBody JSONObject paramJb){
        Plan plan = new Plan();
        this.mapJsonToObj(paramJb, plan);
        JSONArray selectedCkList = paramJb.getJSONArray("selectedCkIds");
        planService.updatePlan(plan, selectedCkList);
        return this.success();
    }

    @RequestMapping("deleteplan")
    public String deletePlan(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        planService.deletePlan(planId);
        return this.success();
    }

    @RequestMapping("fabuplan")
    public String fabuPlan(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        planService.faBuPlan(planId);
        return this.success();
    }

    @RequestMapping("getplantypeList")
    public String getPlanTypeList(){
        List<HashMap<String,Object>> zq = PlanZq.getList();
        List<HashMap<String,Object>> ywlx = PlanYwlx.getList();
        List<HashMap<String,Object>> pdlx = PlanPdlx.getList();
        JSONObject jb = new JSONObject();
        jb.put("zq", zq);
        jb.put("ywlx", ywlx);
        jb.put("pdlx", pdlx);
        return this.success(jb);
    }

    @RequestMapping("listfabuplan")
    public String listFaBuPlan(){
        List<Plan> planList = planService.listSelfPlan(this.getUserId());
        return this.success(planList);
    }

    /*统计用*/
    @RequestMapping("getplancangkuonce")
    public String getPlanCangKuOnce(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        List<HashMap<String, Object>> res = planService.getPlanCangKuOnce(planId);
        return this.success(res);
    }
    @RequestMapping("getplancangkumonth")
    public String getPlanCangKuMonth(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        String month = paramJb.getString("month");
        Plan plan = planService.getPlan(planId);
        Date startDate = DateUtil.parseDate(month + "-" + plan.getCdefine1());
        Date endDate = DateUtil.offsetDay(DateUtil.parseDate(month + "-" + plan.getCdefine2()), 1) ;
        List<HashMap<String, Object>> res = planService.getPlanCangKuZq(planId, startDate, endDate);
        return this.success(res);
    }
    @RequestMapping("getplancangkujidu")
    public String getPlanCangKuJidu(@RequestBody JSONObject paramJb){
        Integer planId = paramJb.getIntValue("id");
        Plan plan = planService.getPlan(planId);
        Integer year = paramJb.getInteger("year");
        Integer quarter = paramJb.getInteger("quarter");
        int calcMonth = (quarter - 1) * 3 + Integer.parseInt(plan.getCdefine3());
        Date startDate = DateUtil.parseDate(year + "-" + calcMonth + "-" + plan.getCdefine1());
        Date endDate = DateUtil.parseDate(year + "-" + calcMonth + "-" + plan.getCdefine2());
        List<HashMap<String, Object>> res = planService.getPlanCangKuZq(planId, startDate, endDate);
        return this.success(res);
    }
}
